<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Matek 1</title>
		<style type="text/css">
			.addit td {
				font-family: monospace;
				font-size: 12pt;
				width: 2em;
			}
			.addit input {
				border: 1px solid black;
				font-family: monospace;
				font-size: 12pt;
				width: 2em;
			}
			.addit .spacer {
				width: 5em;
			}
		</style>
		<script type="text/javascript">

		var addit = {
			tasks : {},

			maxVal : 20,
			minVal : 1,
			size : 2,
			
			createAddTask : function(value) {
				return Math.round(Math.random() * (this.maxVal - value));
			},

			createSubTask : function(value) {
				return Math.round(Math.random() * (value - this.minVal));
			},

			createTask : function() {
				var i;
				var task = {
						num : [],
						op  : [],
						pos : -1,
						value : 0,
						answer : -1
					};
				
				for (i = 0; i < this.size; i++) {
					var v;
					if (Math.random() >= 0.5) {
						v = this.createAddTask(i == 0 ? this.minVal : task.value);
						if (i == 0 && v == 0) v = Math.round(this.minVal + Math.random() * 5);
						task.num.push(v);
						task.op.push(1);
						task.value += v;
					} else {
						v = this.createSubTask(i == 0 ? this.maxVal : task.value);
						if (i == 0 && v == 0) v = Math.round(this.maxVal - Math.random() * 5);
						task.num.push(v);
						task.op.push(-1);
						task.value += i == 0 ? v : -v;
					}
				}
				task.pos = Math.floor(Math.random() * (this.size + 1));
				return task;
			},

			createField : function(x, task, id) {
				var field;
				if (x == task.pos) {
					field = '<input id="' + id + '" onchange="javascript: addit.setAnswer(this);" maxlength="2"/>';
				} else {
					field = x >= task.num.length ? task.value : task.num[x];
				}

				return field;
			},

			createTable : function(tableID) {
				var table = document.createElement("TABLE");
				table.border = 0;
				table.className = "addit";
				var idx = 0;
				for (var j = 0; j < 4; j++) {
					var tr = document.createElement("TR");

					for (var i = 0; i < 3; i++) {
						var td = document.createElement("TD");
						td.className = "spacer";
						tr.appendChild(td);

						var task = this.createTask();
						var inputID = tableID + "_" + idx;
						for (var x = 0; x < task.num.length; x++) {
							td = document.createElement("TD");
							td.innerHTML = this.createField(x, task, inputID);//td.innerHTML = task.num[x];
							tr.appendChild(td);

							td = document.createElement("TD");
							if (x + 1 >= task.num.length) {
								td.innerHTML = "=";
							} else {
								td.innerHTML = task.op[x + 1] > 0 ? "+" : "-";
							}

							tr.appendChild(td);
						}
						td = document.createElement("TD");
						td.innerHTML = this.createField(x, task, inputID);//td.innerHTML = task.value;
						tr.appendChild(td);

						this.tasks[inputID] = task;

						idx++;
					}

					table.appendChild(tr);
				}
				return table;
			},

			setAnswer : function(o) {
				this.tasks[o.id].answer = o.value;
			},

			check : function() {
				for (id in this.tasks) {
					var o = document.getElementById(id);
					if (o != null) {
						var v = this.tasks[id].pos >= this.tasks[id].num.length ? this.tasks[id].value : this.tasks[id].num[this.tasks[id].pos];
						o.style.backgroundColor = (this.tasks[id].answer == v ? "#88ff88" : "#ff8888");
					}
				}
			}
		};

		var machine = {
			tasks : {},

			maxVal : 20,
			minVal : 0,

			constant : -1,
			cpos : -1,
			
			createAddTask : function(value) {
				return Math.round(Math.random() * (this.maxVal - value));
			},
			
			createSubTask : function(value) {
				return Math.round(Math.random() * (value - this.minVal));
			},

			createTask : function(op) {
				var task = {
					v : [-1, -1, -1],
					answer : -1,
					pos : -1
				};

				if (this.cpos > -1) {
					task.v[this.cpos] = this.constant;
					switch (this.cpos) {
						case 0:
							task.v[1] = (op > 0 ? this.createAddTask : this.createSubTask)(this.constant);
							task.v[2] = task.v[0] + op * task.v[1];
							task.pos = Math.floor(Math.random() * 2) + 1;
							break;
						case 1:
							task.v[0] = op > 0 ? this.createAddTask(this.constant) : this.createSubTask(this.maxVal - this.constant) + this.constant;
							task.v[2] = task.v[0] + op * task.v[1];
							task.pos = Math.floor(Math.random() * 2) * 2;
							break;
						default:
							task.v[0] = op > 0 ? this.createSubTask(this.constant) : this.createSubTask(this.maxVal - this.constant) + this.constant;
							task.v[1] = task.v[0] - op * task.v[2];
							task.pos = Math.floor(Math.random() * 2);
							break;
					}
				} else {
					task.v[0] = Math.round(Math.random() * (this.maxVal - this.minVal) * 0.9 + (this.maxVal - this.minVal) * 0.05);
					task.v[1] = Math.round((op > 0 ? this.createAddTask : this.createSubTask)(task.v[0]));
					task.v[2] = task.v[0] + op * task.v[1];
					task.pos = Math.floor(Math.random() * 3);
				}
				
			}
		};

		/*window.onload = function() {
			var a = document.getElementById("addiv");
			a.appendChild(addit.createTable("addit0"));
		};*/

		function generate() {
			document.getElementById("startiv").style.display = "none";
			addit.size = new Number(document.getElementById("sizev").value);
			addit.maxVal = new Number(document.getElementById("maxiv").value);
			document.getElementById("addiv").appendChild(addit.createTable("addit0"));
		}
		
		</script>
	</head>
	<body>
	
		<div id="startiv">méret (2 v 3): <input id="sizev" value="2"/> számkör: <input id="maxiv" value="20"/> <input type="button" value="mehet" onclick="javascript: generate();"/></div>
		<div id="addiv"></div><br/>
		
		<div id="machiv"></div>
		<input type="button" value="Ellenőrzés" onclick="javascript: addit.check();"/>
		
	
	</body>
</html>